Shader "Tideshader/4_法线"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Color",Color) = (1,1,1,1)
        _NormalTex("法线贴图",2d) = "bump"{}
        _NormalScale("法线强度",float) = 1
        _Specular("高光颜色",Color) = (1,1,1,1)
        _Gloss("高光强度",Range(8,256)) = 20
    }
    SubShader
    {
        Tags
        {
            "LightMode"="ForwardBase"
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 tangent:TANGENT;
                float4 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float3 lightDir:TEXCOORD1;
                float3 viewDir:TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            sampler2D _NormalTex;
            float4 _NormalTex_ST;
            float _NormalScale;
            float4 _Specular;
            float _Gloss;


            /// 法线强度公式
            half3 NormalScale(half3 n, half nScale)
            {
                half3 normal;
                normal.xy = n.xy * nScale;
                normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
                return normal;
            }

            /// 兰伯特漫反射公式
            half3 LambertLight(half3 n, half3 l)
            {
                return max(0, dot(n, l));
            }

            /// BlinnPhone高光公式
            half3 BlinnPhongLight(half3 n, half3 v, half3 l, half gloss)
            {
                fixed3 h = normalize(l + v);
                return pow(max(0, dot(n, h)), gloss);
            }

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                // float2 u1 = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.xy = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.uv.xy * _NormalTex_ST.xy + _NormalTex_ST.zw;
                // float2 v1 = v.uv.xy * _NormalTex_ST.xy + _NormalTex_ST.zw;

                // o.uv

                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
                fixed3 worldBiNormal = cross(worldNormal, worldTangent) * v.tangent.w;

                float3x3 worldToTangent = float3x3(worldTangent, worldBiNormal, worldNormal);

                o.lightDir = mul(worldToTangent, WorldSpaceLightDir(v.vertex));
                o.viewDir = mul(worldToTangent, WorldSpaceViewDir(v.vertex));

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 tangentLightDir = normalize(i.lightDir);
                fixed3 tangentViewDir = normalize(i.viewDir);
                fixed4 packedNormal = tex2D(_NormalTex, i.uv.zw);
                fixed3 tangentNormal = UnpackNormal(packedNormal);
                tangentNormal = NormalScale(tangentNormal, _NormalScale);

                fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
                fixed3 diffuse = _LightColor0.rgb * albedo * LambertLight(tangentNormal, tangentLightDir);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * BlinnPhongLight(tangentNormal, tangentViewDir, tangentLightDir, _Gloss);

                return fixed4(ambient + diffuse + specular, 1);
            }
            ENDCG
        }
    }
}